{*
  Object Pascal simple windows registry manipulation routines
  @Author  Maciej Zagozda (1998-2008)
  @Licence Microsoft Reciprocal License (Ms-RL)
  @Website http://www.codeplex.com/intelitechdelphirtns/
  @Version 2008/04/20 Added documentation support
}

unit RegistryRoutines;

interface

Uses
  Classes,
  Dialogs,
  Messages,
  SysUtils,
  Registry,
  Windows;


function RegisterFileExtension( strnExtension, strnIdentifier, strnDescription, strnIcon, strnExecute: AnsiString; boolOverwrite: Boolean ): Boolean;
function TranslateRootKey( strnString: AnsiString ): HKey;
function NotifyRegistryChange: Boolean;

implementation

//_____________________________________________________________________________

function RegisterFileExtension( strnExtension, strnIdentifier, strnDescription, strnIcon, strnExecute: AnsiString; boolOverwrite: Boolean ): Boolean;
var
  Registry: TRegistry;
begin
  
  Result := True;
  if Copy( strnExtension, 1, 1 ) <> '.' then strnExtension := '.' + strnExtension;

  try
    Registry := TRegistry.Create;
    Registry.RootKey := HKEY_CLASSES_ROOT;

    Result := True;

    if Registry.OpenKey( strnExtension, False ) and not boolOverwrite then Exit;

    if Registry.OpenKey( strnExtension, True ) then
    begin
      Registry.WriteString( '', strnIdentifier );
    end
    else
    begin
      Result := False;
      Exit;
    end;//if
    Registry.CloseKey;

    if Registry.OpenKey( strnIdentifier, True ) then
    begin
      Registry.WriteString( '', strnDescription );
    end
    else
    begin
      Result := False;
      Exit;
    end;//if
    Registry.CloseKey;

    if Registry.OpenKey( strnIdentifier + '\DefaultIcon', True ) then
    begin
      Registry.WriteString( '', strnIcon );
    end
    else
    begin
      Result := False;
      Exit;
    end;//if
    Registry.CloseKey;

    if Registry.OpenKey( strnIdentifier + '\Shell\Open\Command', True ) then
    begin
      Registry.WriteString( '', strnExecute );
    end
    else
    begin
      Result := False;
      Exit;
    end;//if
    Registry.CloseKey;

    Registry.Free;
  except
    Result := False;
  end;//except
end;

function TranslateRootKey( strnString: AnsiString ): HKey;
begin

  Result := HKEY_CURRENT_USER;
  if ( AnsiCompareText( 'HCR', strnString ) = 0 ) or ( AnsiCompareText( 'HKEY_CLASSES_ROOT', strnString ) = 0 )then Result := HKEY_CLASSES_ROOT;
  if ( AnsiCompareText( 'HLM', strnString ) = 0 ) or ( AnsiCompareText( 'HKEY_LOCAL_MACHINE', strnString ) = 0 ) then Result := HKEY_LOCAL_MACHINE;
  if ( AnsiCompareText( 'HU', strnString ) = 0 ) or ( AnsiCompareText( 'HKEY_USERS', strnString ) = 0 ) then Result := HKEY_USERS;
  if ( AnsiCompareText( 'HCC', strnString ) = 0 ) or ( AnsiCompareText( 'HKEY_CURRENT_CONFIG', strnString ) = 0 ) then Result := HKEY_CURRENT_CONFIG;

end;


{*
  Notifies all application about a change in registry section
  @return True
*}

function NotifyRegistryChange: Boolean;
begin
  Result := True;
  SendMessage(HWND_BROADCAST, WM_WININICHANGE, 0, LongInt(PChar('RegistrySection')));
end;


end.
